using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_BallotCompetition : BaseTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }

    }

    public override void DataBind()
    {
        Acms.Bll.PostControl drawDateCtrl = CurrentPost.FindPostControl("DrawDate");
        if (drawDateCtrl != null)
        {
            Acms.AcmsControls.TextContentControlProperties prop = drawDateCtrl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (prop != null)
            {
                DrawDate = prop.Text;
            }
        }
        Acms.Bll.PostControl contactDateCtrl = CurrentPost.FindPostControl("ContactDate");
        if (contactDateCtrl != null)
        {
            Acms.AcmsControls.TextContentControlProperties prop = contactDateCtrl.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (prop != null)
            {
                ContactDate = prop.Text;
            }
        } 
        base.DataBind();
    }

    protected string DrawDate
    {
        get { return ViewState["DrawDate"] as string; }
        set { ViewState["DrawDate"] = value; }
    }

    protected string ContactDate
    {
        get { return ViewState["ContactDate"] as string; }
        set { ViewState["ContactDate"] = value; }
    }

    protected void btnEnter_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btnEnter = sender as ImageButton;
        Image imgThankyou = viewDownload.FindControl("imgThankyou") as Image;
        Image imgAlreadyEntered = viewDownload.FindControl("imgAlreadyEntered") as Image;

        if (btnEnter != null && imgThankyou != null && imgAlreadyEntered != null)
        {
            //check not entered already
            Acms.Bll.UserPostVote vote = Acms.Bll.UserPostVote.GetByUserIdAndPostId(Page.User.Identity.User.Id, this.CurrentPostId);
            if (vote == null)
            {
                vote = Acms.Bll.UserPostVote.CreateInstance(Page.User.Identity.User.Id, this.CurrentPostId);
                vote.Score = 1;
                vote.Update();
                Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(Page.User.Identity.User, MapPath("~/Emails/BallotEntryEmail.xml"));
                email.Format("EntryDate", DateTime.Now.ToLongDateString());
                email.Format("CompetitionName", CurrentPostDisplayName);
                email.Format("DrawDate", DrawDate);
                email.Format("ContactDate", ContactDate);
                email.Send();

                btnEnter.Visible = false;
                imgThankyou.Visible = true;
                imgAlreadyEntered.Visible = false;

            }
            else
            {
                btnEnter.Visible = false;
                imgThankyou.Visible = false;
                imgAlreadyEntered.Visible = true;
            }
        }
    }


}
